package com.zf.actuator.endpoint.info;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.actuate.info.JavaInfoContributor;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 自定义 一个InfoContributor，用来给 info 端点的响应中增信息
 * <p>
 * 参考{@link JavaInfoContributor} 类的定义
 *
 * <pre>
 * {@code
 * {
 *     "user": {
 *         "name": "你好",
 *         "password": 123456
 *     },
 *     "address": "ddddd",
 *     "java": {
 *         "vendor": "Oracle Corporation",
 *         "version": "11.0.7",
 *         "runtime": {
 *             "name": "Java(TM) SE Runtime Environment",
 *             "version": "11.0.7+8-LTS"
 *         },
 *         "jvm": {
 *             "name": "Java HotSpot(TM) 64-Bit Server VM",
 *             "vendor": "Oracle Corporation",
 *             "version": "11.0.7+8-LTS"
 *         }
 *     },
 *     "myCustom": {
 *         "add": "额外信息",
 *         "id": "123"
 *     },
 *     "myCustom2": "信息2"
 * }
 * }
 * </pre>
 */
// 自定义的InfoContributor中的信息默认会出现在 info 端点的响应中，不受 management.info.env.enabled 配置属性的影响
@Component
public class MyCustomInfoContributor implements InfoContributor {

    /**
     * 给 info 端点的响应中添加信息
     */
    @Override
    public void contribute(Info.Builder builder) {
        HashMap<String, Object> info = new HashMap<>();
        info.put("add", "额外信息");
        info.put("id", "123");
        // 信息的key -> value, 如果key相同，则只保留最后一个设置的值
        builder.withDetail("myInfo", info);
        builder.withDetail("myInfo2", "信息2");
    }
}
